package cn.itcast.controller.system;

import cn.itcast.controller.BaseContorller;
import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseContorller {

    @Autowired
    private ModuleService moduleService;



    @RequestMapping(value = "/list",name = "分页展示模块数据显示")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo pageInfo =moduleService.findPage(pageNum,pageSize);
        request.setAttribute("page",pageInfo);
        return "system/module/module-list";
    }

    @RequestMapping(value = "/toAdd",name = "跳转添加模块")
    public String toAdd(){
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        return "system/module/module-add";
    }


    @RequestMapping(value = "/edit",name = "保存模块")
    public String edit(Module module){

        if (StringUtils.isEmpty(module.getId())){//id为空
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);

        }else {//id不为空
            moduleService.update(module);
        }


        return "redirect:/system/module/list.do";
    }

    @RequestMapping(value = "/toUpdate",name = "进入模块修改页面")
    public String update(String id){
        List<Module> moduleList = moduleService.findAll();
        request.setAttribute("menus",moduleList);
        Module module = moduleService.findById(id);
        request.setAttribute("module",module);
        return "system/module/module-add";
    }

    @RequestMapping(value = "/delete",name = "进入模块修改页面")
    public String delete(String id){

        moduleService.deleteById(id);

        return "redirect:/system/module/list.do";
    }


}
